package com.qf.wangqing.sportsnews.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.BR;
import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.adapter.recommendadapter.TitleType2CommentRecyclerAdapter;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleCommentComments;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleCommentRoot;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleCommentSize;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleRecommendRecs;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleRecommendRoot;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleViewData;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleViewRecommend;
import com.qf.wangqing.sportsnews.bean.newstitletype2.TitleViewRoot;
import com.qf.wangqing.sportsnews.databinding.ActivityTitleType2Binding;
import com.qf.wangqing.sportsnews.databinding.Titletype2topitemBinding;
import com.qf.wangqing.sportsnews.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TitleType2Activity extends AppCompatActivity implements Callback<TitleViewRoot>, View.OnClickListener {
    private ActivityTitleType2Binding binding;
    private Map<Class<?>,Pair<Integer,Integer>> map;
    private TitleType2CommentRecyclerAdapter adapter;
    private MyApplication myApplication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      binding = DataBindingUtil.setContentView(this,R.layout.activity_title_type2);
        init();
        Intent intent = getIntent();
        String link = intent.getStringExtra("link");
        Log.i("link","link2："+link);
        myApplication = (MyApplication) getApplication();
        myApplication.getNewsService().getType2View(link,"json","4","false").enqueue(this);
        myApplication.getNewsService().getType2Recommend(link,"json","4","false").enqueue(new Callback<TitleRecommendRoot>() {
            @Override
            public void onResponse(Call<TitleRecommendRoot> call, Response<TitleRecommendRoot> response) {
                TitleRecommendRoot body = response.body();
                if (body.getData().getRecs() != null && body.getData().getRecs().size() != 0) {
                    TitleViewRecommend recommend = new TitleViewRecommend();
                    adapter.add(1, recommend);
                    adapter.addAll(body.getData().getRecs());
                }
            }

            @Override
            public void onFailure(Call<TitleRecommendRoot> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(TitleType2Activity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void init() {
        map = new HashMap<>();
        map.put(TitleViewData.class,Pair.create(R.layout.titletype2topitem, BR.data));
        map.put(TitleViewRecommend.class,Pair.create(R.layout.titletype2recommend,BR.recommend));
        map.put(TitleRecommendRecs.class,Pair.create(R.layout.titletype2re_long,BR.recs));
        map.put(TitleCommentSize.class,Pair.create(R.layout.titletype2commentsize,BR.commentsize));
        map.put(TitleCommentComments.class,Pair.create(R.layout.titletype2comments,BR.comments));
        adapter = new TitleType2CommentRecyclerAdapter(this,new ArrayList(),map);
        binding.recycler.setAdapter(adapter);
        binding.recycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        binding.imageBlack.setOnClickListener(this);
    }
    @Override
    public void onResponse(Call<TitleViewRoot> call, Response<TitleViewRoot> response) {
       TitleViewRoot body = response.body();
        //加载webview
//        Titletype2topitemBinding webBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.titletype2topitem, this.binding.recycler, false);
//        String content = body.getData().getText_content();
//        Log.i("content","content:"+content);
//        List<String> images = response.body().getData().getText_images();
//        Matcher matcher = Pattern.compile("\\$\\{lx-(\\w+)-?(\\d*)\\}").matcher(content);
//        StringBuffer buffer = new StringBuffer("<style>img{max-width:100%}</style>");
//        while (matcher.find()) {
//            switch (matcher.group(1)) {
//                case "br":
//                    matcher.appendReplacement(buffer, "<br/>");
//                    break;
//                case "image":
//                    matcher.appendReplacement(buffer, String.format(Locale.getDefault(), "<img src=\"%s\"/>", images.get(Integer.parseInt(matcher.group(2)))));
//                    break;
//            }
//        }
//        matcher.appendTail(buffer);

//        webBinding.text.setText(content);

        if (body.getData() != null) {
            adapter.add(0,body.getData());
        }
//        TitleViewRecommend recommend = new TitleViewRecommend();
//        adapter.add(1, recommend);

        //请求评论数量
        myApplication.getNewsService().getCommentSize(body.getData().getDashboard_data(),"json","4","false").enqueue(new Callback<TitleCommentSize>() {
            @Override
            public void onResponse(Call<TitleCommentSize> call, Response<TitleCommentSize> response) {
                TitleCommentSize body1 = response.body();
                TitleType2Activity.this.binding.setSize(body1);
                if (body1!=null  && body1.getData() != 0) {
                    adapter.add(body1);
                }
            }

            @Override
            public void onFailure(Call<TitleCommentSize> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(TitleType2Activity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        //请求评论数据
        myApplication.getNewsService().getComments(body.getData().getDashboard_id(),"json","4","false").enqueue(new Callback<TitleCommentRoot>() {
            @Override
            public void onResponse(Call<TitleCommentRoot> call, Response<TitleCommentRoot> response) {
                TitleCommentRoot body2 = response.body();
//                Log.i("titlecomment","we"+body2.getData().getComments().get(0).getContent());
                if (body2.getData().getComments() != null && body2.getData().getComments().size() != 0) {
                    adapter.addAll(body2.getData().getComments());
                }
            }

            @Override
            public void onFailure(Call<TitleCommentRoot> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(TitleType2Activity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onFailure(Call<TitleViewRoot> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        finish();
        overridePendingTransition(R.anim.left, R.anim.exit);
    }
}
